//
//  CustomerController.swift
//  Order
//
//  Created by Lazyloading on 2020/7/29.
//  Copyright © 2020 lazyloading. All rights reserved.
//

import UIKit

class CustomerController: UIViewController ,UITableViewDelegate,UITableViewDataSource{
    
    let kCustomerCellID = "kCustomerCellID"
    var dataArray = Array<[String : Any]>();
    
    lazy var tableView : UITableView = {
        let table = UITableView.init(frame: self.view.bounds);
        
        table.backgroundColor = ZMColor.Gray.gray1;
        table.delegate = self;
        table.dataSource = self;
        table.registerNib("CustomerCell", kCustomerCellID);
        table.showsVerticalScrollIndicator = false;
        table.separatorStyle = .none;
        table.estimatedRowHeight = 0;
        table.estimatedSectionFooterHeight = 0;
        table.estimatedSectionHeaderHeight = 0;
        if #available(iOS 11.0, *) {
            table.contentInsetAdjustmentBehavior = .never
        } else {
            // Fallback on earlier versions
            self.automaticallyAdjustsScrollViewInsets = false;
        };
        return table;
        
    }();
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.loadDefaultSetting();
        self.initSubViews();
        self.getCustomerInfo();
    }
    
    // MARK: 👉🏻 🐷Life cycle🐷
    // MARK: 👉🏻 🐷System Delegate🐷

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        self.dataArray.count;
     }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 60;
    }

     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let dict = self.dataArray[indexPath.row];
        let cell =  tableView.dequeueReusableCell(withIdentifier: kCustomerCellID) as! CustomerCell;
        
        
        let infoName = dict["infoName"] as! String;
        let infoValue = dict["infoValue"] as!  String;
        cell.titleLabel.text = infoName;
        cell.descLabel.text = infoValue;
        
        return cell;
     }
    // MARK: 👉🏻 🐷Custom Delegate🐷
    // MARK: 👉🏻 🐷Event  Response🐷
    // MARK: 👉🏻 🐷Private Methods🐷
    func getCustomerInfo()  {
        weak  var weakSelf = self;
        MoyaRequest(.customer, success: { (response : BaseResponse<[[String : Any]]>) in
            if response.respCode == 200{
                weakSelf?.dataArray = response.data!;
                weakSelf?.tableView.reloadData();
            }else{
                
            }
        }) { (erro) in
            
        }
        
    }
    // MARK: 👉🏻 🐷Lazy loading🐷
    // MARK: 👉🏻 🐷Init SubViews🐷
    func loadDefaultSetting()  {
        self.view.backgroundColor = .white;
        self.navigationItem.title = "联系客服";
    }
    func initSubViews()  {
        self.view.addSubview(self.tableView);
        
        self.tableView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview();
        }
        
    }
}
